package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import service.NotificationManager;
import service.RecommendationManager;

import models.Handshake;
import models.Handshake.Status;
import models.Notification;
import models.Offer;
import models.Request;
import models.Service;
import models.ServiceHasParticipants;
import models.Tag;
import models.TagCloudItem;
import models.User;

public class Application extends BaseController
{   
    public static void index() throws IOException
	{
    	int recentUserCount =5;
	    int recentHandshakeCount = 5;
	    List<User> newUsers = User.getNewUsers(recentUserCount);
	    //List<Handshake> newHandshakes = Handshake.find("(status='ACCEPTED' or status='STARTED' or status='DONE') order by creationDate desc").fetch(recentHandshakeCount);
	    
		// build a tag cloud for services.
		
		// instantiate a new cloud instance (a wiser way is
		// to use a map instead of a list; but for compatibility
		// reasons, I'll stick on the list).
	    List<TagCloudItem> tagOffersCloud = new ArrayList<TagCloudItem>();
		
		// ???
		int listFlag = 0;
		
		// for each service;
		Iterator<Object> it = Offer.all().fetch().iterator();
		while (it.hasNext()) {
			// get the current service.
			Offer service = (Offer) it.next();
			
			// for each related tag of the service;
			for (Tag tag : service.tags) {
				// is tag is already found in the tag cloud?
				int tagIndex = tagOffersCloud.indexOf(new TagCloudItem(tag.name));
				
				// if found;
				if (-1 != tagIndex) {
					// increment the count of the tag occurrence.
					((TagCloudItem) tagOffersCloud.get(tagIndex)).count++;
				}
				
				// if not found;
				else {
					// ???
					listFlag++;
					int cssFlag = 0;
					if (listFlag % 2 == 0) {
			   	    	cssFlag = (listFlag + 2) / 2;
					} else {
						cssFlag = (listFlag + 1) / 2;
					}
					
					// create a new tag cloud item for the tag.
					TagCloudItem tagCloudItem = new TagCloudItem();
					tagCloudItem.name = tag.name;
					tagCloudItem.count = new Long(1);
					tagCloudItem.hyperlink = "/offers/search?phrase=" + tag.name;
					tagCloudItem.CssClass = "tag_" + cssFlag;
					
					// add the item to the tag cloud.
					tagOffersCloud.add(tagCloudItem);
				}
			}
		}
		
		// instantiate a new cloud instance (a wiser way is
		// to use a map instead of a list; but for compatibility
		// reasons, I'll stick on the list).
	    List<TagCloudItem> tagRequestsCloud = new ArrayList<TagCloudItem>();
		
		// ???
		listFlag = 0;
		
		// for each service;
		Iterator<Object> itR = Request.all().fetch().iterator();
		while (itR.hasNext()) {
			// get the current service.
			Request service = (Request) itR.next();
			
			// for each related tag of the service;
			for (Tag tag : service.tags) {
				// is tag is already found in the tag cloud?
				int tagIndex = tagRequestsCloud.indexOf(new TagCloudItem(tag.name));
				
				// if found;
				if (-1 != tagIndex) {
					// increment the count of the tag occurrence.
					((TagCloudItem) tagRequestsCloud.get(tagIndex)).count++;
				}
				
				// if not found;
				else {
					// ???
					listFlag++;
					int cssFlag = 0;
					if (listFlag % 2 == 0) {
			   	    	cssFlag = (listFlag + 2) / 2;
					} else {
						cssFlag = (listFlag + 1) / 2;
					}
					
					// create a new tag cloud item for the tag.
					TagCloudItem tagCloudItem = new TagCloudItem();
					tagCloudItem.name = tag.name;
					tagCloudItem.count = new Long(1);
					tagCloudItem.hyperlink = "/requests/search?phrase=" + tag.name;
					tagCloudItem.CssClass = "tag_" + cssFlag;
					
					// add the item to the tag cloud.
					tagRequestsCloud.add(tagCloudItem);
				}
			}
		}

		// shuffler clouds.
		Collections.shuffle(tagOffersCloud);
		Collections.shuffle(tagRequestsCloud);
		
		// set the view variables.
		renderArgs.put("tagOffersCloud", tagOffersCloud); 
		renderArgs.put("tagRequestsCloud", tagRequestsCloud);
		
		//Recommendation
		User user = getConnectedUser();
    	RecommendationManager recManager = new RecommendationManager();
    	List<Service> recServices = recManager.RecommendServices(user);
    	renderArgs.put("recommendedServices", recServices);
		
	    //Render
	    render(newUsers);
	}

    public void BindRecommendedOffers(){
    	
    }
    
    public static void register() {
		render();
    }

    public static void about() {
	render();
    }

    public static void faq() {
	render();
    }

    public static void contact() {
	render();
    }
    
    public static void termsOfService() {
	render();
    }

    public static void showUserPhoto(Long userId) {
	User user = User.findById(userId);
	renderBinary(user.photo.get());
    }

}
